package cn.edu.xhu.library_management.book.service.impl;

import cn.edu.xhu.library_management.book.entity.LmBook;
import cn.edu.xhu.library_management.book.entity.dto.BookRecordDTO;
import cn.edu.xhu.library_management.book.mapper.LmBookMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.xhu.library_management.book.entity.LmBorrow;
import cn.edu.xhu.library_management.book.service.LmBorrowService;
import cn.edu.xhu.library_management.book.mapper.LmBorrowMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 21086
* @description 针对表【lm_borrow(借阅表)】的数据库操作Service实现
* @createDate 2023-05-02 09:31:11
*/
@Service
public class LmBorrowServiceImpl extends ServiceImpl<LmBorrowMapper, LmBorrow>
    implements LmBorrowService{
    @Resource
    private LmBookMapper bookMapper;
    @Override
    public List<BookRecordDTO> reBorrow(String account) {
        List<BookRecordDTO> dto = new ArrayList<>();
        QueryWrapper<LmBorrow> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LmBorrow::getBoStatus,"0"); // 未归还
        List<LmBorrow> borrows = this.list(queryWrapper);
        borrows.forEach(borrow -> {
            BookRecordDTO recordDTO = new BookRecordDTO();
            recordDTO.setBorrow_id(borrow.getId()); // 设置归还表Id
            LmBook book = bookMapper.selectById(borrow.getBookId());
            BeanUtils.copyProperties(book,recordDTO);
            dto.add(recordDTO);
        });
        return dto;
    }

    @Override
    public List<BookRecordDTO> noReBorrow(String account) {
        List<BookRecordDTO> dto = new ArrayList<>();
        QueryWrapper<LmBorrow> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LmBorrow::getBoStatus,"1"); // 未归还
        List<LmBorrow> borrows = this.list(queryWrapper);
        borrows.forEach(borrow -> {
            BookRecordDTO recordDTO = new BookRecordDTO();
            recordDTO.setBorrow_id(borrow.getId()); // 设置归还表Id
            LmBook book = bookMapper.selectById(borrow.getBookId());
            BeanUtils.copyProperties(book,recordDTO);
            dto.add(recordDTO);
        });
        return dto;
    }

}




